﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ePlanet.ArchiveMange.Services;
using ePlanet.ArchiveMange.Models;
using DreamBuilder.Core.Container;
using ePlanet.ArchiveBorrow.Services;

namespace Web.Views.ArchiveBorrow
{
    public partial class ArchiveBorrowStatistics : PageBase
    {
        public int PeopleCount = 0;
        public int ArchiveCount = 0;
        public int PaperCount = 0;
        public int PaperReturnCount = 0;
        public int PaperLendCount = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                BindArchiveType();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                var beginDate = DateTime.Now;
                var endDate = DateTime.Now;
                var archiveType = Convert.ToInt32(this.ddlarchiveType.SelectedValue);

                if (!(!string.IsNullOrEmpty(this.txtBeginDate.Text) && DateTime.TryParse(this.txtBeginDate.Text, out beginDate)))
                    throw new Exception("请选择起始时间");

                if (!(!string.IsNullOrEmpty(this.txtEndDate.Text) && DateTime.TryParse(this.txtEndDate.Text, out endDate)))
                    throw new Exception("请选择起始时间");

                var service = UContainer.Resolve<IArchiveBorrowService>();
                var result = service.GetrchiveBorrowStatistics(beginDate, endDate, archiveType);

                if (result != null)
                {
                    PeopleCount = result["PeopleCount"];
                    ArchiveCount = result["ArchiveCount"];
                    PaperCount = result["PaperCount"];
                    PaperReturnCount = result["PaperReturnCount"];
                    PaperLendCount = result["PaperLendCount"];
                }
                else
                {
                    throw new Exception("查询结果出错");
                }
            }
            catch (Exception ex)
            {
                Alert(ex.Message);
            }
        }

        private void BindArchiveType()
        {
            IArchiveMenuService IArchiveMenu = UContainer.Resolve<IArchiveMenuService>();
            ArchiveMangeModal.ARCHIVEMENUDataTable dtArchiveMenu =
               IArchiveMenu.GetAllArchiveMenu();

            this.ddlarchiveType.DataTextField = "ArchiveMenu";
            this.ddlarchiveType.DataValueField = "TypeID";

            this.ddlarchiveType.DataSource = dtArchiveMenu;
            this.ddlarchiveType.DataBind();
        }
    }
}